package com.xiyou.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiyou.weblog.common.domain.dos.TagDo;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author xiyou
 * @description 文章标签
 */
public interface TagMapper extends BaseMapper<TagDo> {

    default TagDo selectByName(String categoryName) {
        //构建查询条件
        LambdaQueryWrapper<TagDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TagDo::getName, categoryName);

        //执行
        return selectOne(wrapper);
    }

    /**
     * 标签分页查询
     */
    default Page<TagDo> selectPageList(Long current, long size, String name, LocalDate startDate, LocalDate endDate) {
        // 分页对象
        Page<TagDo> page = new Page<>(current, size);
        // 构建查询条件
        LambdaQueryWrapper<TagDo> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .like(Objects.nonNull(name),TagDo::getName,name)//参数一：判断为不为空，为空直接滤过
                .ge(Objects.nonNull(startDate),TagDo::getCreateTime,startDate)//大于
                .le(Objects.nonNull(endDate),TagDo::getCreateTime,endDate)//小于
                .orderByDesc(TagDo::getCreateTime);// 根据时间倒序排序

        return selectPage(page, wrapper);
    }

    /**
     * 根据标签关键字模糊查询
     */
    default List<TagDo> selectByKey(String key) {
        LambdaQueryWrapper<TagDo> wrapper = new LambdaQueryWrapper<>();
        // 构造模糊条件(默认忽略大小写)
        wrapper.like(TagDo::getName, key).orderByDesc(TagDo::getCreateTime,TagDo::getId);

        return selectList(wrapper);
    }

    /**
     * 根据标签 ID 批量查询
     */
    default List<TagDo> selectByIds(List<Long> tagIds) {
        return selectList(Wrappers.<TagDo>lambdaQuery()
                .in(TagDo::getId, tagIds));
    }


}
